package com.bank.dao;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.bank.entity.Organization;
import com.bank.entity.Teller;


@Repository("organizationDao")
public class OrganizationDaoImpl   implements OrganizationDao {

	
	Logger logger =Logger.getLogger(OrganizationDaoImpl.class);
	
	
	
	@Autowired
	SessionFactory sessionFactory;

	public void save(Organization orientation) {
		Session session  =  sessionFactory.getCurrentSession();
		session.save(orientation);
		session.flush();
	}

	public void update(Organization orientation) {
		// TODO Auto-generated method stub
		Session session  =  sessionFactory.getCurrentSession();
		session.update(orientation);
		session.flush();
	}

	public Organization getOrientation(String number) {
		// TODO Auto-generated method stub
		String sql = "from  Organization where number='"+number+"'";
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createQuery(sql);
		List<Organization> list = query.list();
		Organization organization = null;
		if(list.size()==0)
			return organization;
		for(Organization organizationTmp:list){
			organization = organizationTmp;
		}
		return organization;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> getOrganizationList(Map<String, Object> params) {
		// TODO Auto-generated method stub
		String sql = "from Organization where 1=1 ";
		Integer offsetInt  = (Integer)params.get("offset");
		Integer limitInt = (Integer)params.get("limit");
		String number = (String)params.get("number");
		String seniorSuper  = (String)params.get("senior_super");
		String accountingSuper = (String)params.get("accounting_super");
		String level = (String)params.get("level");
		String status = (String)params.get("status");
		if(number!=null && number.equals("")==false){
			sql = sql+" and number ='"+number+"'";
		}
		if(seniorSuper != null && seniorSuper.equals("")==false){
			sql = sql+" and  senior_super='"+seniorSuper+"'";
		}
		if(accountingSuper != null && accountingSuper.equals("")==false){
			sql = sql+"  and accounting_super='"+accountingSuper+"'";
		}
		
		if(level != null && level.equals("")==false){
			sql = sql+"  and level='"+level+"'";
		}
		if(status != null && status.equals("")==false){
			sql = sql+"  and status='"+status+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(offsetInt);
		query.setMaxResults(limitInt);
		List<Object> accountList = query.list();	
		return accountList;		
	}

	public Integer getOrganizationListCount(Map<String, Object> params) {
		String sql = "select count(*) from Organization where 1=1 ";
		String number = (String)params.get("number");
		String seniorSuper  = (String)params.get("senior_super");
		String accountingSuper = (String)params.get("accounting_super");
		String level = (String)params.get("level");
		String status = (String)params.get("status");
		if(number!=null && number.equals("")==false){
			sql = sql+" and number ='"+number+"'";
		}
		if(seniorSuper != null && seniorSuper.equals("")==false){
			sql = sql+" and  senior_super='"+seniorSuper+"'";
		}
		if(accountingSuper != null && accountingSuper.equals("")==false){
			sql = sql+"  and accounting_super='"+accountingSuper+"'";
		}
		
		if(level != null && level.equals("")==false){
			sql = sql+"  and level='"+level+"'";
		}
		if(status != null && status.equals("")==false){
			sql = sql+"  and status='"+status+"'";
		}
		Session  session = sessionFactory.getCurrentSession();
		int totalRecord = Integer.parseInt(session.createQuery(sql).uniqueResult().toString());
		return totalRecord;
	}
	

}
